package com.tju.resource.oauth.controller.tb;

import cn.hutool.json.JSONObject;
import com.tju.resource.annotation.Log;
import com.tju.resource.business.tb_shipment.report.TbShipReportService;
import com.tju.resource.tool.enum_utils.FunctionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@RestController
public class TbShipReportController {

    private static final String prefix = "shiprepo";

    @Autowired
    TbShipReportService service;

    @PostMapping(prefix + "/export")
    @PreAuthorize("hasAuthority('sys:goods:ship:export')")
    @Log(type = FunctionType.Export)
    public void export(@RequestBody JSONObject condition, HttpServletResponse response) {
        try {
            response.setContentType("application/octet-stream; charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("Shipment" + Math.random() + ".xls", StandardCharsets.UTF_8));
            service.wrSimple(condition, response.getOutputStream());
//            return ResultUtil.success(ResultEnum.SUCCESS);
        } catch (Exception e) {
//            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.toString());
        }
    }

}
